from PIL import Image, ImageFilter
import svgwrite

import cv2

# def jpg_to_png(jpg_file, png_file):
#     img = cv2.imread(jpg_file)
#     cv2.imwrite(png_file, img)
#     # 将图像转换为灰度图像
#     gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#     _, binary_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)
#     edges = cv2.Canny(binary_image, 100, 200)
#


    
def convert_image_to_svg(jpg_file, svg_file):
    # 打开 JPG 文件
    img = Image.open(jpg_file)
    # 转换为灰度图
    # img = img.convert('L')
    # 应用边缘检测
    img = img.filter(ImageFilter.FIND_EDGES)
    # 获取图像的尺寸
    width, height = img.size
    # 创建 SVG 文件
    # dwg = svgwrite.Drawing(svg_file, profile='tiny', size=(width, height))
    dwg = svgwrite.Drawing(svg_file, profile='tiny')
    # 将 JPG 图像逐像素转换为 SVG 矩形
    for x in range(img.width):
        for y in range(img.height):
            r, g, b = img.getpixel((x, y))
            color = f'#{r:02x}{g:02x}{b:02x}'
            dwg.add(dwg.rect((x, y), (1, 1), fill=color))
        # 保存 SVG 文件
    dwg.save()
    # for y in range(height):
    #     for x in range(width):
    #         # 获取像素值，值越高表示越亮
    #         pixel_value = img.getpixel((x, y))
    #         if pixel_value > 128:  # 调整阈值以控制细节
    #             continue  # 亮的部分不画
    #         # 如果是黑色，则添加一个小矩形
    #         print(x)
    #         dwg.add(dwg.rect(insert=(x, y), size=(1, 1), fill='black'))

        # 保存 SVG 文件
    # dwg.save()
    print("图片生成完成")

# 使用示例
# 示例调用
input_image = 'logo_new.png'  # 替换为您的图片路径
output_svg = 'logo_new.svg'  # 指定输出的 SVG 路径
convert_image_to_svg(input_image, output_svg)
# jpg_to_svg('login.jpg', 'output.svg')


